package com.yuanfeng.base.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yuanfeng.base.entity.BaseDistrictEntity;
import com.yuanfeng.base.mapper.BaseDistrictMapper;
import com.yuanfeng.base.service.BaseDistrictService;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.excel.Constants;
import com.yuanfeng.commoms.vo.shop.ShopAllBaseDistrictVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BaseDistrictServiceImpl
 * @Description 地区
 * @Author cf
 * @Date 2023/6/14 15:02
 * @Version 1.0
 */
@Service
public class BaseDistrictServiceImpl extends ServiceImpl<BaseDistrictMapper, BaseDistrictEntity>
    implements BaseDistrictService{

    @Autowired
    private BaseDistrictMapper baseDistrictMapper;


    @Override
    public ResponseResult queryDistinct(Integer districtParentId) {

        List<BaseDistrictEntity> districts = this.baseMapper.selectList(new LambdaQueryWrapper<BaseDistrictEntity>()
                                            .eq(null!=districtParentId,BaseDistrictEntity::getDistrictParentId,districtParentId));

        return ResponseResult.success(districts);
    }

    @Override
    public String queryProvinceport(String provinceId) {
        return baseDistrictMapper.queryProvinceport(provinceId);
    }

    /**
     * 获取地址所有的父分类id
     *
     * @param allMenu
     * @param catId
     * @return
     */
    public List<Integer> getDistrictList(List<ShopAllBaseDistrictVO> allMenu, Integer catId) {
        List<Integer> allChildCatIds = Lists.newArrayList();

        if (!CollectionUtils.isEmpty(allMenu)) {
            for (ShopAllBaseDistrictVO catMenu : allMenu) {
                if (catMenu.getDistrictId().equals(catId)) {
                    allChildCatIds.add(catMenu.getDistrictId());
                    if (!catMenu.getDistrictParentId().equals(0)) {
                        getDistrictChild3(allMenu, catMenu.getDistrictParentId(), allChildCatIds);
                    }
                    break;
                }

            }
        }
        return allChildCatIds;

    }

    /**
     * 找出子菜单
     *
     * @return
     */
    private List<Integer> getDistrictChild3(List<ShopAllBaseDistrictVO> allMenu, Integer parentId,
                                            List<Integer> allChildCatIds) {

        for (ShopAllBaseDistrictVO catMenu : allMenu) {
            if (catMenu.getDistrictId().equals(parentId)) {
                allChildCatIds.add(catMenu.getDistrictId());
                parentId = catMenu.getDistrictParentId();
                break;
            }
        }

        if (!parentId.equals(0)) {
            getDistrictChild3(allMenu, parentId, allChildCatIds);
        }

        return allChildCatIds;
    }

    @Override
    public ResponseResult getAddressString(Integer provinceId, Integer cityId, Integer districtId) {

        //省或直辖市名称
        String provinceName= this.baseMapper.selectById(String.valueOf(provinceId)).getDistrictName();
        //市或直辖市区名称
        String cityName = this.baseMapper.selectById(String.valueOf(cityId)).getDistrictName();
        //区或直辖市区街道名称
        String districtName = this.baseMapper.selectById(String.valueOf(districtId)).getDistrictName();
        String address=provinceName+cityName+districtName;
        address=address.replace("null","");
        return ResponseResult.success(address);
    }

    @Override
    public ResponseResult queryAllDistrict() {

        List<ShopAllBaseDistrictVO> districtList = null;

        // 从缓存中取
        String value2 = RedisUtil.get(Constants.CACHE_DISTRICTS_SHOP_ADDRESS_ALL_BASE2);
        if (null != value2 && !value2.equals("")) {
            districtList = JSON.parseArray(value2, ShopAllBaseDistrictVO.class);
        } else {
            // 从缓存中取
            List<ShopAllBaseDistrictVO> allMenu = null;
            String value = RedisUtil.get(Constants.CACHE_DISTRICTS_SHOP_ADDRESS_ALL_BASE);
            if (null != value && !value.equals("")) {
                allMenu = JSON.parseArray(value, ShopAllBaseDistrictVO.class);
            } else {
                allMenu = baseDistrictMapper.selectAllDistrict();
                // 设置缓存
                RedisUtil.set(Constants.CACHE_DISTRICTS_SHOP_ADDRESS_ALL_BASE, JSON.toJSONString(allMenu),
                        RedisUtil.EXRP_MONTH);
            }
            districtList = getDistrictList2(allMenu);
            // 设置缓存
            RedisUtil.set(Constants.CACHE_DISTRICTS_SHOP_ADDRESS_ALL_BASE2, JSON.toJSONString(districtList),
                    RedisUtil.EXRP_MONTH);
        }

        return ResponseResult.success(districtList);
    }

    @Override
    public List<String> selectByIdList(List<Integer> districtIds) {
        return baseDistrictMapper.selectByIdList(districtIds);
    }

    @Override
    public List<String> queryRegionName(Map<String, Integer> districtMap) {
        return this.baseMapper.queryRegionName(districtMap);
    }


    /**
     * 查询所有的区域
     *
     * @param rootMenus
     * @return
     */
    public List<ShopAllBaseDistrictVO> getDistrictList2(List<ShopAllBaseDistrictVO> rootMenus) {
        // 先找到所有的一级菜单
        List<ShopAllBaseDistrictVO> childMenu = Lists.newArrayList();
        for (ShopAllBaseDistrictVO menu : rootMenus) {

            if (menu.getDistrictParentId().equals(0)) {
                ShopAllBaseDistrictVO menu2 = new ShopAllBaseDistrictVO();
                menu2.setDistrictId(menu.getDistrictId());
                menu2.setDistrictName(menu.getDistrictName());
                menu2.setCheckedSubMenus(new ArrayList<>());
                menu2.setCheckAll(false);
                menu2.setIsIndeterminate(false);
                List<ShopAllBaseDistrictVO> child1 = getDistrictChild2(menu.getDistrictId(), rootMenus);
                menu2.setChildDistrict(child1);
                childMenu.add(menu2);
            }
        }
        return childMenu;

    }


    /**
     * 找出子菜单
     *
     * @param parentId
     * @param rootMenu
     * @return
     */
    private List<ShopAllBaseDistrictVO> getDistrictChild2(Integer parentId, List<ShopAllBaseDistrictVO> rootMenu) {
        // 子菜单
        List<ShopAllBaseDistrictVO> childList = new ArrayList<ShopAllBaseDistrictVO>();

        for (ShopAllBaseDistrictVO menu : rootMenu) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            if (!StringUtils.isEmpty(menu.getDistrictParentId())) {
                if (menu.getDistrictParentId().equals(parentId)) {
                    ShopAllBaseDistrictVO district2 = new ShopAllBaseDistrictVO();
                    district2.setDistrictId(menu.getDistrictId());
                    district2.setDistrictName(menu.getDistrictName());
                    childList.add(district2);
                }
            }
        }
        return childList;
    }


}




